import React, { useReducer, useContext, createContext } from 'react'
/* 存在store中的数据,类型定义 */
interface store {

}
/* 先看本地有没有数据 */
const getSession = () => {
	return JSON.parse(sessionStorage.getItem('userinfo') as any )
}
/* 获取初始数据 */
const state = getSession() ? getSession() : {}
/* 创建reducer函数,用于修改store中的数据 */
const reducer = (state:any, action:any) => {
	switch (action.type) {
		case 'MENU':
			return Object.assign({}, state, action.menu)

		default:
			throw new Error('Unexpected action')
	}
}
/* 创建传值的组件 */
const ReducerTemplate = createContext(null)
/* 暴露传值的根组件 */
export const ReducerContext = (props:any) => {
	/* 生成store数据 */
	const store = useReducer(reducer, state)

	return <ReducerTemplate.Provider value={store as any}> {props.children} </ReducerTemplate.Provider>
}
/* 这里可以获取到用户的状态管理数据 */
const useGetStore = () => {
	const state:any = useContext(ReducerTemplate)
	return state
}
export default useGetStore
